package me.sdevil507.org.po;


import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import me.sdevil507.supports.jpa.BaseJpaSnowflakeIdAndAuditEntity;
import org.hibernate.annotations.Formula;

import javax.persistence.*;
import java.util.Set;

/**
 * 机构角色表[OrgRolePo]
 *
 * @author sdevil507
 * created on 2020-06-23 18:15:44
 */
@Entity
@Table(name = "biz_org_role")
@Data
@EqualsAndHashCode(callSuper = true, exclude = {"users", "resourceRel"})
@ToString(exclude = {"users", "resources"})
public class OrgRolePo extends BaseJpaSnowflakeIdAndAuditEntity {

    /**
     * 角色名称
     */
    @Column
    private String name;

    /**
     * 角色编码
     */
    @Column
    private String code;

    /**
     * 角色描述
     */
    @Column
    private String description;

    /**
     * 是否预置
     */
    @Column
    private boolean preset = false;

    /**
     * 排序号
     */
    @Column
    private Integer sortNum;

    /**
     * 部门id
     */
    @Column
    private Long deptId;

    /**
     * 部门name
     */
    @Formula("(SELECT bod.title from biz_org_dept bod where bod.id=dept_id)")
    private String deptName;

    /**
     * 包含用户
     */
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "biz_org_user_role_rel",
            joinColumns = {
                    @JoinColumn(name = "role_id", referencedColumnName = "id", foreignKey = @ForeignKey(name = "none"))},
            inverseJoinColumns = {
                    @JoinColumn(name = "user_id", referencedColumnName = "id", foreignKey = @ForeignKey(name = "none"))})
    private Set<OrgUserPo> users;

    /**
     * 包含资源
     */
//    @ManyToMany(fetch = FetchType.EAGER)
//    @JoinTable(name = "biz_org_role_res_rel",
//            joinColumns = {
//                    @JoinColumn(name = "role_id", referencedColumnName = "id", foreignKey = @ForeignKey(name = "none"))},
//            inverseJoinColumns = {
//                    @JoinColumn(name = "res_id", referencedColumnName = "id", foreignKey = @ForeignKey(name = "none"))})
//    private Set<OrgResourcePo> resources;


    /**
     * 包含机构
     */
    @ManyToMany(fetch = FetchType.LAZY)
    @JoinTable(name = "biz_org_role_org_rel",
            joinColumns = {
                    @JoinColumn(name = "role_id", referencedColumnName = "id", foreignKey = @ForeignKey(name = "none"))},
            inverseJoinColumns = {
                    @JoinColumn(name = "org_id", referencedColumnName = "id", foreignKey = @ForeignKey(name = "none"))})
    private Set<OrgPo> orgs;

    /**
     * 包含资源
     */
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "role")
    private Set<OrgRoleResourceRelPo> resourceRel;
}